#ifndef MYMESSAGE_H
#define MYMESSAGE_H

#include <QWidget>
#include <QCloseEvent>
#include <QListWidgetItem>
#include <QColor>
#include <QMap>
#include <QTcpSocket> // 添加头文件
#include "pack.h"
#include "frienditem.h"
#include <QListWidget>
#include "abs.h"
namespace Ui {
class myMessage;
}

class myMessage : public QWidget
{
    Q_OBJECT

private slots:
    void on_onlineList_itemClicked(QListWidgetItem *item);
    void on_pushButton_send_clicked();
    void on_pushButton_exit_clicked();

// 确保函数声明完整
public:
    explicit myMessage(QWidget *parent = nullptr);
    ~myMessage();
    void setSocket(QTcpSocket *s);
    void setCurrentUsername(const QString &username);
    void showReceivedMessage(const QString &sender, const QString &message, const QString &time, bool showTime = true);
    void handleOnlineList(const QStringList &onlineUsers);
    void handleOnlineList(const Pack& pack);

private:
    Ui::myMessage *ui;
    QTcpSocket *socket;
    QString currentUsername;
    QString currentChatUser;
    Abs* msgabs;
    QMap<QString, QStringList> chatHistory;
    void updateOnlineListUI(const QStringList& users);
    void addMessageToChat(const QString &sender, const QString &message, const QString &time, bool isSelf);
    void addMessageToChat(const QString &text, const QColor &color, Qt::Alignment alignment);
    void closeEvent(QCloseEvent *event) override;
};

#endif // MYMESSAGE_H
